﻿using System;

namespace DotNetty.Framework.BaseTriggers
{
    /// <summary>
    /// 工人的路由
    /// </summary>
    [AttributeUsage(AttributeTargets.Class)]
    public class WorkerAttribute : Attribute
    {
        public string Route { get; set; }
        public WorkerAttribute(string route) { this.Route = this.TrimAndToLower(route); }

        /// <summary>
        /// 小写，去除所有空格
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected string TrimAndToLower(string value)
        {
            return value.ToLower().Replace(" ", "");
        }
    }

    /// <summary>
    /// 方法路由
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class MethodRouteAttribute : Attribute
    {
        public string Route { get; set; }

        /// <summary>
        /// 是否需要解析参数
        /// </summary>
        public bool IsHavParameter { get; set; }

        /// <summary>
        /// 忽略其它Route
        /// </summary>
        public bool IgnoreOtherRoute { get; set; }

        public MethodRouteAttribute(string route)
        {
            if (route.IndexOf("$") > -1)
            {
                var routeArray = route.Split("$");
                this.Route = this.TrimAndToLower(routeArray[0]);
                this.IsHavParameter = true;
            }
            else
            {
                this.Route = this.TrimAndToLower(route);
            }
        }

        /// <summary>
        /// 小写，去除所有空格
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected string TrimAndToLower(string value)
        {
            return value.ToLower().Replace(" ", "");
        }
    }
}
